/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-01-27 11:16:59
 * @filename : iscroll.js
 * @version : 
 * @description : 
 */

;(function($){
   
    var touch = {move:0};

    var pull = {

        init : function(opt) {
            this.opt = opt;
            this.bindEvt(opt);
        },

        bindEvt : function(opt) {                        
            var tthis = this;

            opt.dom.on('touchstart',function(e){
                touch.x1 = e.touches[0].pageX;
                touch.y1 = e.touches[0].pageY;
                touch.x2 = e.touches[0].pageX;
                touch.y2 = e.touches[0].pageY;
                touch.targetTop = e.target.offsetTop;
                touch.bool = true; 
            }).on('touchmove',function(e){
                if (touch.bool) {
                    if (touch.pull) {
                        e.preventDefault();
                        e.stopPropagation(); 
                    }
                    touch.x2 = e.touches[0].pageX;
                    touch.y2 = e.touches[0].pageY;
                    touch.direction = tthis.touchDirection(touch.x1,touch.x2,touch.y1,touch.y2);
                    if (touch.direction == 'Up' || touch.direction == 'Down') {           
                        tthis['pull'+ touch.direction+'Move'](e);
                    }
                }
            }).on('touchend',function(e){
                 if (touch.bool) {
                    if (touch.direction == 'Up' || touch.direction == 'Down') {                 
                       tthis['pull'+ touch.direction+'End'](e);
                    }               
                 }
                 touch.bool = false;
            });      
       },

       touchDirection : function(x1, x2, y1, y2) {
            return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down');
       },

       pullMoveConditionY : function(moveNum) {
            if (moveNum < 0 ) {
                moveNum = 0;
            } else if (moveNum > this.opt.dom.parent().height()) {
                moveNum = this.opt.dom.parent().height();
            }
            return moveNum;
       },

       pullDownMove : function(e) {
            console.log('pullDownMove');
            /*
             * scrollTop的值延迟响应move的值造成的，造成计算有误差坑爹的BUG，使用scrollTop进入了一个错误的误区，应该使用targetTop。targetTop才是准确的.
             */     

            var boxTop = this.opt.dom.parent().offset().top;
       
            var moveNum = Math.abs(touch.y2 - touch.y1);
   
            //计算滚动与上拉差值,注意这里有点不好理解
            var cssMove = Math.abs(touch.targetTop - (touch.y1-boxTop));

            //判断触发下拉的条件
            if (moveNum >= cssMove) {
                 touch.pull = true;
                 this.actionCss(this.pullMoveConditionY(moveNum - cssMove),0); 
            } else {
                 touch.pull = false;
                 this.actionCss(0,0);//偶尔会有2，3个像素没有归位，直接修复。
            }

       },

       pullUpMove : function(e) {
            console.log('pullUpMove');
            var moveNum = Math.abs(touch.y1 - touch.y2),                  
                maxHeight = this.opt.dom.height();
            //容器底部的TOP值
            var boxTop = this.opt.dom.parent().height() + this.opt.dom.parent().offset().top;
            //计算滚动与上拉差值,注意这里有点不好理解
            var cssMove = Math.abs(maxHeight - touch.targetTop - (boxTop - touch.y1));

            if (moveNum >= cssMove) {
                 touch.pull = true;
                 this.actionCss(-this.pullMoveConditionY(moveNum - cssMove),0);
            } else {
                 touch.pull = false;
                 this.actionCss(0,0);//偶尔会有2，3个像素没有归位，直接修复。
            }

      },

       pullDownEnd : function(e) {
            console.log('pullDownEnd');
            
            this.actionCss(0,600);
            
       },

       pullUpEnd : function(e) {
            console.log('pullDownEnd');
       
            this.actionCss(0,600);
       },

       actionCss : function(move,time) {
           this.opt.dom.attr("style","transition: "+time+"ms; -webkit-transition: "+time+"ms ; transform: translate(0px, "+move/2+"px) translateZ(0px)");
       }
    }    


    $.fn.pull = function(opt) {
         opt.dom = this;
         pull.init(opt);      
    }

})(Zepto);


